package com.zu.commu.local.process;

import com.zu.commu.context.ServerContext;
import com.zu.commu.exception.CommuException;
import com.zu.commu.interf.Order;
import com.zu.commu.remote.session.Session;
import com.zu.commu.remote.session.SessionManager;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class SendRemote extends Thread
{
  private static Logger log = Logger.getLogger(" com.automic.commu.local.process.SendRemote");
  private String gprsId;
  private String commandId;
  private byte[] orderData;
  private Order order;

  public void setSendData(String gprsId, String commandId, byte[] orderData, Order order)
  {
    this.gprsId = gprsId;
    this.commandId = commandId;
    this.orderData = orderData;
    this.order = order;
  }

  public void send()
  {
    SessionManager qm = SessionManager.instance();
    Session se = qm.getSessionByGprsId(this.gprsId);

    if (se == null) {
      log.error("向远端GPRS:" + this.gprsId + "发送数据出错,其网络连接会话为null ,可能还未上线！");
      return;
    }

    try
    {
      if (Level.INFO.isGreaterOrEqual(log.getEffectiveLevel())) {
        try {
          String hex = byte2Hex(this.orderData);
          log.info("向远端GPRSID:" + this.gprsId + "发送命令：" + hex);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      se.write(this.orderData);
    } catch (Exception e) {
      log.error("向远端GPRSID:" + se.getAttribute("gprsId") + "发送数据出错,网络写出异常！" + e.getMessage(), e);
    }
  }

  public void sendSingle()
  {
    SessionManager qm = SessionManager.instance();
    Session se = qm.getSessionByGprsId(this.gprsId);

    if (se == null) {
      log.error("向远端GPRS:" + this.gprsId + "发送数据出错,其网络连接会话为null ,可能还未上线！");
      return;
    }
    se.putInCommandId(this.commandId, this.order);
    try {
      if (Level.INFO.isGreaterOrEqual(log.getEffectiveLevel())) {
        try {
          String hex = byte2Hex(this.orderData);
          log.info("向远端GPRSID:" + this.gprsId + "发送命令：" + hex);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      se.write(this.orderData);
    } catch (Exception e) {
      log.error("向远端GPRSID:" + se.getAttribute("gprsId") + "发送数据出错,网络写出异常！" + e.getMessage(), e);
    }
  }

  public void run()
  {
    String meterProtocol = (String)ServerContext.GPRSID_meterProtocol.get(this.gprsId);
    if (meterProtocol == null) {
      log.error("没有得到GPRSID为:" + this.gprsId + "的测控器协议！");
    }
    String sendOrderTimes = (String)ServerContext.meterProtocol_sendOrderTimes.get(meterProtocol);
    if (sendOrderTimes == null) {
      log.error("没有得到测控器协议为:" + meterProtocol + "命令重发次数！采用默认次数3 .");
      sendOrderTimes = "3";
    }
    int sotimes = Integer.parseInt(sendOrderTimes);

    String sendOrderWaitTime = (String)ServerContext.meterProtocol_sendOrderWaitTime.get(meterProtocol);
    if (sendOrderWaitTime == null) {
      log.error("没有得到测控器协议为:" + meterProtocol + "命令重发间隔时长！采用默认时长1分钟 .");
      sendOrderWaitTime = "1";
    }
    Double timeTemp = new Double(Double.parseDouble(sendOrderWaitTime) * 60.0D * 1000.0D);
    long sowtime = timeTemp.longValue();

    SessionManager qm = SessionManager.instance();
    Session se = qm.getSessionByGprsId(this.gprsId);

    if (se == null) {
      log.error("向远端GPRS:" + this.gprsId + "发送数据出错,其还未上线！");
      return;
    }

    se.putInCommandId(this.commandId, this.order);
    try
    {
      int count = 0;
      while (count <= sotimes) {
        if (count == sotimes + 1)
        {
          break;
        }
        count++;

        if (Level.INFO.isGreaterOrEqual(log.getEffectiveLevel())) {
          try {
            String hex = byte2Hex(this.orderData);
            log.info("向远端GPRSID:" + this.gprsId + "发送命令：" + hex);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }

        se.write(this.orderData);
        try {
          sleep(sowtime);
        } catch (Exception e) {
          try {
            sleep(sowtime); } catch (Exception localException1) {
          }
        }
        if (!se.isExistCommandId(this.commandId))
        {
          break;
        }
      }

    }
    catch (Exception e)
    {
      log.error("向远端发送数据出错," + e.getMessage(), e);
    }
  }

  private String byte2Hex(byte[] b)
    throws CommuException
  {
    String rString = "";
    String temp = "";
    try {
      for (int i = 0; i < b.length; i++) {
        int c = b[i];
        temp = Integer.toHexString(c & 0xFF);
        if (temp.length() == 1) {
          temp = "0" + temp;
        }
        rString = rString + temp;
      }
    } catch (Exception e) {
      throw new CommuException().setMessage("字节数组转换成十六进制的字符串出错！", null);
    }
    return rString;
  }
}